<script setup>
import { reactive, onMounted } from 'vue'
import { getRoleAllApi, getRoleListApi, delRoleApi } from '@/api/role.js'
import { useRoleStatus } from '@/hooks'
import role from './role.vue'
//时间戳转时间
import dayjs from 'dayjs'
import { ElMessage } from 'element-plus'
const roleStatus = useRoleStatus()
const roleRef = ref('')

const queryData = reactive({
  current: 1,
  pageSize: 5,
  create_time: '',
  id: '',
  name: '',
  description: '',
  status: ''
})
const total = ref(0)
const list = ref([])
const initData = async () => {
  let { data } = await getRoleListApi(queryData)
  // console.log(res);
 
  data.rows = data.rows.map(item => {
    item.create_time = dayjs(item.create_time).format('YYYY/MM/DD HH:mm:ss')
    return item
  })
  total.value = data.count
  list.value = data.rows
}
onMounted(() => {
  initData()
})

//搜索
const onSearch = () => {
  queryData.current = 1
  initData()
}
//重置
const onReset = () => {
  Object.assign(queryData, {
    current: 1,
    pageSize: 5,
    create_time: '',
    id: '',
    name: '',
    description: '',
    status: ''
  })
  initData()
}
//每页数量切换
const sizeChange = (val) => {
  queryData.pageSize = val
  initData()
}
//页码切换
const CurrentChange = val => {
  queryData.current = val
  initData()
}

// 修改角色
const editRole = (id) => {
  console.log(id);
  roleRef.value.isShow = true
  roleRef.value.replyData(id)
}

// 删除角色
const delRole =  async (id) => {
  await delRoleApi(id)
  ElMessage.success('删除成功')
  initData()
}

</script>
<template>
  <div>
    <div class="app-container">
      <span style="font-weight: 700;">角色管理</span> <span>用于系统角色管理，并且对角色进行授权管理</span>
      <el-card>
        <!-- 搜索区域 -->
        <el-form label-width="80px" :inline="true" label-position="top">

          <el-form-item label="角色名称">
            <el-input slot="prepend" placeholder="请输入" v-model="queryData.name">
            </el-input>
          </el-form-item>

          <el-form-item label="角色状态">
            <el-select clearable slot="prepend" placeholder="请选择" v-model="queryData.status">
              <el-option v-for="item in roleStatus" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>

          <el-form-item style="display: flex; align-items: end;">
            <div></div>
            <div>
              <el-button type="primary" @click="onSearch">搜索</el-button>
              <el-button @click="onReset">取消</el-button>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="app-container">
      <el-card>
        <!-- 工具区域 -->
        <el-row type="flex" justify="space-between">
          <div></div>
          <div>
            <el-button type="primary" @click="roleRef.isShow = true">新增角色</el-button>
          </div>
        </el-row>
        <br>
        <!-- 表格区域 -->
        <el-table :data="list" border stripe>
          <el-table-column label="ID" prop="id" width="100px"></el-table-column>
          <el-table-column label="角色名称" prop="name"></el-table-column>
          <el-table-column label="角色描述" prop="description"></el-table-column>
          <el-table-column label="状态" prop="status">
            <template #default="{ row }">
              <span v-if="row.status == 1">启用</span>
              <span v-else>禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="create_time"></el-table-column>
          <el-table-column label="操作">
            <template #default="{row}">
              <el-link class="link" :underline="false" type="primary" @click="editRole(row.id)">编辑</el-link>
              <el-link class="link" :underline="false" type="primary" @click="delRole(row.id)">删除</el-link>
            </template>

          </el-table-column>
        </el-table>
        <br>
        <!-- 分页区域 -->
        <el-row type="flex" justify="space-between">
          <div></div>
          <el-pagination v-model:current-page="queryData.current" v-model:page-size="queryData.pageSize"
            :page-sizes="[5, 10, 20]" :small="small" :disabled="disabled" :background="background"
            layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="sizeChange"
            @current-change="CurrentChange" />
        </el-row>
        <!-- 新增修改组件 -->
        <!-- <usage ref="usageRef" @updateList="initData" /> -->
        <role ref="roleRef" @updateList="initData"/>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.link {
  margin-left: 15px;
}
</style>